package kgc.crm.controller;

import com.alibaba.fastjson.JSON;
import kgc.crm.annotation.VerificationToken;
import kgc.crm.model.Business;
import kgc.crm.model.BusinessProduct;
import kgc.crm.service.IBusinessService;
import kgc.crm.util.Constant;
import kgc.crm.util.RequestUtil;
import kgc.crm.util.ResultUtil;
import kgc.crm.vo.InsertBusinessVo;
import kgc.crm.vo.LeadsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("business")
@RestController
@CrossOrigin
public class BusinessController {
    @Autowired
    private IBusinessService businessService;

    @VerificationToken
    @PostMapping("add")
    public ResponseEntity add(@RequestBody InsertBusinessVo vo, HttpServletRequest request) {
        String apiToken = RequestUtil.getApiToken(request);
        ResponseEntity add = businessService.add(vo, apiToken);
        //Object
        return add;
    }

    @VerificationToken
    @PostMapping("select/list")
    public ResponseEntity list(@RequestBody LeadsVo vo, HttpServletRequest request) {

        //Object
        return businessService.selectListById(vo, RequestUtil.getApiToken(request));
    }



    public static void main(String[] args) {
        InsertBusinessVo vo = new InsertBusinessVo();
        //设置商机属性
        Business business = new Business();
        business.setTypeId(2);
        business.setStatusId(2);
        business.setNextTime(new Date(System.currentTimeMillis()+ Constant.EXPIR_TIME_HH_6));
        business.setDealDate(new Date(System.currentTimeMillis()+ Constant.EXPIR_TIME_HH_2));
        business.setCustomerId(2);
        business.setBusinessName("狗队长人生第三单");
        business.setMoney(new BigDecimal(707.5).setScale(2,BigDecimal.ROUND_HALF_UP));
        business.setDiscountRate(new BigDecimal(0.50).setScale(2,BigDecimal.ROUND_HALF_UP));
        business.setBatchId("010102");
        business.setTotalPrice(new BigDecimal(707.5).setScale(2,BigDecimal.ROUND_HALF_UP));
        vo.setBusiness(business);
        //设置产品属性
        BusinessProduct bp = new BusinessProduct();
        bp.setProductId(2);//产品id
        bp.setPrice(new BigDecimal(200).setScale(2,BigDecimal.ROUND_HALF_UP));
        bp.setSalesPrice(new BigDecimal(180).setScale(2,BigDecimal.ROUND_HALF_UP));
        bp.setNum(4);
        bp.setDiscount(new BigDecimal(0.10).setScale(2,BigDecimal.ROUND_HALF_UP));
        bp.setSubtotal(new BigDecimal(720).setScale(2,BigDecimal.ROUND_HALF_UP));
        List<BusinessProduct> list = new ArrayList<>();
        list.add(bp);


        BusinessProduct bp2 = new BusinessProduct();
        bp2.setProductId(3);//产品id
        bp2.setPrice(new BigDecimal(139).setScale(2,BigDecimal.ROUND_HALF_UP));
        bp2.setSalesPrice(new BigDecimal(139).setScale(2,BigDecimal.ROUND_HALF_UP));
        bp2.setNum(5);
        bp2.setDiscount(new BigDecimal(0.00).setScale(2,BigDecimal.ROUND_HALF_UP));
        bp2.setSubtotal(new BigDecimal(695).setScale(2,BigDecimal.ROUND_HALF_UP));
        list.add(bp2);

        vo.setBusinessProduct(list);
        System.out.println(JSON.toJSONString(vo));
    }
}
